//
//  HZNoticeView.m
//  HZTC_YaDeArt
//
//  Created by MayerF on 17/3/25.
//  Copyright © 2017年 HZTC. All rights reserved.
//

#import "HZNoticeView.h"
#import "HZNoticeTableViewCell.h"
#import "MYUpDownBtn.h"

#define kNoticeRowH 70

@interface HZNoticeView()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *pTableView;
@property (nonatomic, strong)NSArray *pListData;
@property(nonatomic,strong)MYUpDownBtn    *pAlertInfo;
@end

@implementation HZNoticeView

#pragma mark -
#pragma mark 生命周期
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kMYHexToRGB(0xf6f6f6, 1);
        [self p_commonInit];
    }
    return self;
}
#pragma mark -
#pragma mark public
-(void)setNoticeData:(NSArray *)data{
    if (data) {
        _pListData = data;
        [_pTableView reloadData];
    }else {
        [self p_loadAlertInfo];
    }
}
#pragma mark -
#pragma mark private
- (void)p_commonInit
{
    _pTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _pTableView.delegate = self;
    _pTableView.dataSource = self;
    _pTableView.tableFooterView = [UIView new];
    _pTableView.backgroundColor = [UIColor clearColor];
    
    [self addSubview:_pTableView];
    
    [self p_initConstraints];
}

- (void)p_initConstraints
{
    [_pTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
    }];
}

- (void)p_loadAlertInfo{
    if (!_pAlertInfo) {
        _pAlertInfo = [MYUpDownBtn new];
        [_pAlertInfo setImage:[UIImage imageNamed:@"empty"] forState:UIControlStateNormal];
        [_pAlertInfo setImage:[UIImage imageNamed:@"empty"] forState:UIControlStateHighlighted];
        _pAlertInfo.imageView.contentMode = UIViewContentModeCenter;
        [_pAlertInfo setTitle:@"暂无公告" forState:UIControlStateNormal];
        [_pAlertInfo setTitleColor:kMYHexToRGB(0x888888, 1) forState:UIControlStateNormal];
        _pAlertInfo.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_pAlertInfo];
        [_pAlertInfo mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(self);
            make.left.mas_equalTo(self);
            make.right.mas_equalTo(self);
            make.height.mas_equalTo(70);
        }];
    }
    _pAlertInfo.hidden = NO;
}
#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _pListData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HZNoticeModel *mode = _pListData[indexPath.row];
    HZNoticeTableViewCell *cell = [HZNoticeTableViewCell leftCellTableView:tableView indexPath:indexPath cellModel:mode cellHight:kNoticeRowH];
    return cell;
}
#pragma mark -
#pragma mark UITableViewDelegate
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kNoticeRowH;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HZNoticeModel *mode = _pListData[indexPath.row];
    if (_actionClickList) {
        _actionClickList(nil, mode, nil);
    }
}

@end
